import { login, logout, getInfo } from '@/api/user/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// import { reject } from 'core-js/fn/promise'
import router from '@/router';
import { constantRoutes, baseRouter, bossRoutes } from '@/router';
import cloneDeep from 'lodash/cloneDeep'
import Router from 'vue-router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: 'localhost:8080',
    avatar: '',
    resultRoutes: []
  }
}




const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  //这里在处理登录的业务
  async login({ commit,state }, userInfo) {
    // 解构出用户名与密码
    const { username, password } = userInfo
    let result = await login({ username: username.trim(), password: password })

    //注意当前使用的mock数据，而mock数据code成功是20000
    if (result.code == 200) {
      commit('SET_TOKEN', result.data.token)
      setToken(result.data.token)
      // var auth = Number(result.data.auth)
      // if (auth == 0) {

      // } else if (auth == 1) {
      //   // router.addRoutes(adminRouter) 
      //   // 将新的路由添加到 options.routes 中
      //   router.options.routes = router.options.routes.concat(adminRouter)
      //   console.log(router.options.routes);
      //   state.resultRoutes=router.options.routes
      //     console.log(state.resultRoutes);
      // } else if (auth == 2) {
      //   // router.options.routes=baseRouter
      // }
      return "ok"
    } else {
      return Promise.reject(new Error('faile'))
    }

  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

      

        const { name, img ,auth} = data
        var auths=Number(auth)


        if (auths == 0) {
          router.addRoutes(bossRoutes) 
          // 将新的路由添加到 options.routes 中
          router.options.routes = bossRoutes
          state.resultRoutes=router.options.routes 
        } else if (auth == 1) {
          // 这玩意虽然不会加在options的route上，但是貌似会加在底层某个逻辑上，不然单纯的router.options.routes = constantRoutes
          // state.resultRoutes=router.options.routes 是无法在点击侧边栏时转向对应的页面，只会转向空白页
          router.addRoutes(constantRoutes) 
          // 将新的路由添加到 options.routes 中
          router.options.routes = constantRoutes
          state.resultRoutes=router.options.routes 
        } else if (auth == 2) {
          router.addRoutes(baseRouter) 
          router.options.routes = baseRouter
          state.resultRoutes=router.options.routes 
        }

        commit('SET_NAME', name)
        commit('SET_AVATAR', img)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    console.log("1231321231321313212");

    removeToken() // must remove  token  first
    resetRouter()
    commit('RESET_STATE')
    state.resultRoutes=[]
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

